home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 22 / AMUG_22-4.ISO / Files IV / Prog / T / TinyTools-2.sit / TinyTools / FPScroll.c / FPScroll.c
Encoding:
C/C++ Source or Header  |  1997-10-07  |  5.1 KB  |  191 lines  |  [TEXT/MPS ]

  1.  
  2. /* file FPScroll.c
  3.     Routines for using the standard scroll bars with floating
  4.     point numbers providing scrolling accross the range of
  5.     float, double, or whatever...
  6.     Copyright (c) 1997 by John Montbriand.  All Rights Reserved.
  7.     Permission granted for public use.
  8.     Distribute freely in areas where the laws of copyright apply.
  9.     USE AT YOUR OWN RISK.
  10.     DO NOT DISTRIBUTE MODIFIED COPIES.
  11.     Comments/questions/postcards to the author at the address:
  12.         John Montbriand
  13.         P.O. Box. 1133
  14.         Saskatoon Saskatchewan Canada
  15.         S7K 3N2
  16.     or by email at:
  17.         tinyjohn@sk.sympatico.ca
  18.     If you would like to have:
  19.         technical support regarding this file, send a postcard.
  20.     see also:
  21.         http://www3.sk.sympatico.ca/tinyjohn
  22. */
  23.  
  24.  
  25. #include "FPScroll.h"
  26. #include <fp.h>
  27. #include <OSUtils.h>
  28. #include <Memory.h>
  29. #include <MixedMode.h>
  30.  
  31. typedef struct {
  32.     float min, max, value, prevthumb;
  33.     FPScrollActionProc actionproc;
  34.     long reference;
  35. } FPScrollVars;
  36.  
  37. #pragma segment fpscroll
  38.  
  39. ControlHandle NewFPScrollBar(WindowPtr wp, Rect* box, FPScrollActionProc action) {
  40.     ControlHandle control;
  41.     FPScrollVars dv;
  42.     Handle varshandle;
  43.         
  44.         /* set up dv structure */
  45.     dv.min = dv.max = dv.value = 0;
  46.     dv.actionproc = action;
  47.     dv.reference = 0;
  48.     if (PtrToHand(&dv, &varshandle, sizeof(dv)) != noErr) return NULL;
  49.     
  50.         /* create the control */
  51.     control = NewControl(wp, box, "\p", true, 0, 0, kFPScrollPositions, scrollBarProc, (long) varshandle);
  52.     
  53.         /* return the result */
  54.     if (control == NULL) {
  55.         DisposeHandle(varshandle);
  56.         return NULL;
  57.     } else {
  58.         HiliteControl(control, 255);
  59.         return control;
  60.     }
  61. }
  62.  
  63. void DisposeFPScrollBar(ControlHandle control) {
  64.     FPScrollVars **dv;
  65.     dv = (FPScrollVars **) GetCRefCon(control);
  66.     DisposeHandle((Handle) dv);
  67.     DisposeControl(control);
  68. }
  69.  
  70. /* maps a float value from the domain [dmin,dmax] to the range [rmin,rmax] */
  71. static float FPMapValue(float value, float dmin, float dmax, float rmin, float rmax) {
  72.     float slope, b;
  73.     slope = (rmax-rmin) / (dmax-dmin);
  74.     b = rmin - slope * dmin;
  75.     return (slope * value) + b;
  76. }
  77.  
  78. float GetFPCtlMin(ControlHandle control) {
  79.     FPScrollVars **dv;
  80.     dv = (FPScrollVars **) GetCRefCon(control);
  81.     return (**dv).min;
  82. }
  83.  
  84. float GetFPCtlMax(ControlHandle control) {
  85.     FPScrollVars **dv;
  86.     dv = (FPScrollVars **) GetCRefCon(control);
  87.     return (**dv).max;
  88. }
  89.  
  90. float GetFPCtlValue(ControlHandle control) {
  91.     FPScrollVars **dv;
  92.     dv = (FPScrollVars **) GetCRefCon(control);
  93.     return (**dv).value;
  94. }
  95.  
  96. float GetFPThumbValue(ControlHandle control) {
  97.     FPScrollVars **dv;
  98.     dv = (FPScrollVars **) GetCRefCon(control);
  99.     return (**dv).prevthumb;
  100. }
  101.  
  102. long GetFPCtlReference(ControlHandle control) {
  103.     FPScrollVars **dv;
  104.     dv = (FPScrollVars **) GetCRefCon(control);
  105.     return (**dv).reference;
  106. }
  107.  
  108. void SetFPCtlReference(ControlHandle control, long ref) {
  109.     FPScrollVars **dv;
  110.     dv = (FPScrollVars **) GetCRefCon(control);
  111.     (**dv).reference = ref;
  112. }
  113.  
  114. void SetFPCtlMinMax(ControlHandle control, float min, float max) {
  115.     float oldvalue, nextvalue;
  116.     short newvalue;
  117.     FPScrollVars **dv;
  118.     dv = (FPScrollVars **) GetCRefCon(control);
  119.     
  120.         /* ensure the current value is in the new range */
  121.     oldvalue = (**dv).value;
  122.     if (oldvalue < min) 
  123.         nextvalue = min;
  124.     else if (oldvalue > max)
  125.         nextvalue = max;
  126.     else nextvalue = oldvalue;
  127.     (**dv).value = nextvalue;
  128.     
  129.         /* set the minimum and maximum */
  130.     (**dv).min = min;
  131.     (**dv).max = max;
  132.     
  133.         /* display the new settings */
  134.     newvalue = roundtol(FPMapValue(nextvalue, min, max, 0, kFPScrollPositions));
  135.     SetCtlValue(control, newvalue);
  136.     HiliteControl(control, ((min == max) ? 255: 0));
  137. }
  138.  
  139. void SetFPCtlValue(ControlHandle control, float value) {
  140.     FPScrollVars **dv;
  141.     float nextvalue;
  142.     short newvalue;
  143.     dv = (FPScrollVars **) GetCRefCon(control);
  144.     
  145.         /* ensure the value is in the control's range */
  146.     if (value < (**dv).min)
  147.         nextvalue = (**dv).min;
  148.     else if (value > (**dv).max)
  149.         nextvalue = (**dv).max;
  150.     else nextvalue = value;
  151.     (**dv).value = nextvalue;
  152.     
  153.         /* display the new settings */
  154.     newvalue = roundtol(FPMapValue(nextvalue, (**dv).min, (**dv).max, 0, kFPScrollPositions));
  155.     SetCtlValue(control, newvalue);
  156. }
  157.  
  158. static pascal void FPScrollActionProcedure(ControlRef theControl, ControlPartCode partCode) {
  159.     FPScrollVars **dv;
  160.     dv = (FPScrollVars **) GetCRefCon(theControl);
  161.     ((**dv).actionproc)(theControl, partCode, (**dv).reference);
  162. }
  163.  
  164. void TrackFPControl(WindowPtr theWindow, ControlHandle control, Point where) {
  165.     short partcode;
  166.     ControlHandle theControl;
  167.     partcode = FindControl(where, theWindow, &theControl);
  168.     if (partcode != 0 && theControl == control) {
  169.         FPScrollVars **dv;
  170.         short newvalue;
  171.         float nextvalue;
  172.         dv = (FPScrollVars **) GetCRefCon(control);
  173.         if (partcode == inThumb) {
  174.             if (TrackControl(control, where, NULL) == inThumb) {
  175.                 newvalue = GetCtlValue(control);
  176.                 nextvalue = FPMapValue((float) newvalue, 0, kFPScrollPositions, (**dv).min, (**dv).max);
  177.                 if ((**dv).value != nextvalue) {
  178.                     (**dv).prevthumb = (**dv).value;
  179.                     (**dv).value = nextvalue;
  180.                     ((**dv).actionproc)(control, inThumb, (**dv).reference);
  181.                 }
  182.             }
  183.         } else {
  184.             ControlActionUPP local_action;
  185.             local_action = NewControlActionProc(FPScrollActionProcedure);
  186.             TrackControl(control, where, local_action);
  187.             DisposeRoutineDescriptor((UniversalProcPtr) local_action);
  188.         }
  189.     }
  190. }
  191.